package vip.xiaonuo.bi.core.datasource.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import lombok.SneakyThrows;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.bi.modular.dataset.dto.PreviewDto;
import vip.xiaonuo.bi.modular.datasource.dto.Colunm;
import vip.xiaonuo.common.util.CollectionUtil;

import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static vip.xiaonuo.bi.core.datasource.util.ColumnUtils.convertColumnDataByTypeListData;

/**
 * @auther: 顾育司
 * @Description
 * @date: 2024/1/31
 */
public class CSVutils {
    @SneakyThrows
    public static PreviewDto uploadCsv(MultipartFile file, String charset, String colInfoJson) {
        CSVParser csvRecords = null;
        try
        {
            // 获取读取流
            InputStreamReader inputStreamReader = new InputStreamReader(file.getInputStream(), Charset.forName(charset));

            ///  1 文件解读配置
            // 1.1头部第一行作为列名配置
            CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader();
            // 1.2csv分隔符配置
            csvFormat.withDelimiter(',');

            csvRecords = CSVParser.parse(inputStreamReader, csvFormat);
            // 获取列明
            List<String> columns = CollectionUtil.getKeys(csvRecords.getHeaderMap());

            PreviewDto previewDto = new PreviewDto();

            // 2 数据处理
//            定义数据集
            List< List<String>> dataList = new ArrayList<>();
            List<List<String>> finalDataList = dataList;
            StreamSupport.stream(csvRecords.spliterator(), false).forEach(item->{
                try {
                    List<String> data = new ArrayList<>();
                    columns.forEach(column->{
                        data.add(item.get(column));
                    });
                    finalDataList.add(data);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });

            List<Colunm> cols = new ArrayList<>();
            // 如果用户改变列类型返回值
            if(ObjectUtil.isNotEmpty(colInfoJson)){
                // String解析成Map
                cols = JSONArray.parseArray(colInfoJson,Colunm.class);
                Map<String, String> columnTypeMap = cols.stream().collect(Collectors.toMap(k -> k.getName(), v -> v.getType(), (k, v) -> k));
                // 改变数据
                dataList = convertColumnDataByTypeListData(dataList, columns, columnTypeMap);
            }else{
                Map<String, String> finalColumnType =   ColumnUtils.getColumnType(dataList, columns);
                ArrayList  list = new ArrayList<>();
                columns.forEach(item->{
                    Colunm column = new Colunm();
                    column.setName(item);
                    column.setType(finalColumnType.get(item));
                    list.add(column);
                });
                cols.addAll(list);
            }

            previewDto.setData(dataList);
            previewDto.setColums(cols);

            return previewDto;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            csvRecords.close();
        }

        return null;
    }
}
